package ar.com.mooral.activities;

import java.util.Collections;
import java.util.Vector;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.crohn.view.CrohnButton;
import ar.com.mooral.crohn.view.CrohnRadioButton;
import ar.com.mooral.data.MissedMedicationReason;
import ar.com.mooral.data.MissedMedications;
import ar.com.mooral.data.Result;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.MedicationListPojo;
import ar.com.mooral.pojo.MissedMedicationPojo;
import ar.com.mooral.pojo.MissedMedicationReasonPojo;
import ar.com.mooral.pojo.util.PojoComparatorDescription;
import ar.com.mooral.pojo.util.PojoComparatorName;


public class MedicacionPerdidaMotivo extends CrohnActivity {

	private MissedMedications missedMedications;
	
	private MissedMedicationPojo pojo;
	
    protected void onStart() {
		super.onStart();
		
		pojo = (MissedMedicationPojo) CrohnContext.getPojo();
		
		drawGrid();
    }
	
	

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medicacion_perdida_motivo);

        
        missedMedications = new MissedMedications(this);
        

        // VOLVER
        Button backBtn = (Button)findViewById(R.id.btnMedicacionPerdidaMotivoCancelar);
        backBtn.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				finish();
		    }
		});	        
        
        
        // OTRO
        Button otherBtn = (Button)findViewById(R.id.btnMedicacionPerdidaMotivoAgregar);
        otherBtn.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(MedicacionPerdidaMotivoAgregar.class.getPackage().getName(), MedicacionPerdidaMotivoAgregar.class.getName()));
				//startActivity(intent);
				
         		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
         		parentActivity.startChildActivity("MedicacionPerdidaMotivoAgregar", intent);

		    }
		});
        
        
        // REGISTRAR
        Button RegisterBtn = (Button)findViewById(R.id.btnMedicacionPerdidaMotivoRegistrar);
        RegisterBtn.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {

		    	MissedMedicationPojo pojo = (MissedMedicationPojo) CrohnContext.getPojo();
		    	
		    	if (pojo.getReason() == null || pojo.getReason().equals("")) {
		    		Result r = new Result(Result.INFO, "Debe seleccionar el motivo");
		    		showMessage(r);
		    		
		    	} else {

		    		Result r = missedMedications.save(pojo);
		    		if (r.hasError()) {
			        	showMessage(r);
			        } else {
			        	finish();
			        }
		    	}
		    	
		    }
		});
        
    }
    
    
    
    private void drawGrid() {
    	
        MissedMedicationReason missedMedicationReason = new MissedMedicationReason(this);
        Vector data = missedMedicationReason.findAll();
        
    	// Clean before addings
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgMedicacionPerdidaMotivo);
        radioGroup.removeAllViews();
        
        
        Collections.sort((Vector<BasePojo>)data, new PojoComparatorDescription());
        
        for (int i = 0; i < data.size(); i++) {

        	MissedMedicationReasonPojo record = (MissedMedicationReasonPojo) data.get(i);
	        
	        CrohnRadioButton rb = new CrohnRadioButton(this);
	        rb.setPojo(record);
	        
	        String text = record.getDescription();
			if(text.length()>30) text = text.substring(0,30) + "...";
			rb.setText(text);
			rb.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {
    		    	
    		    	MissedMedicationReasonPojo mmp = (MissedMedicationReasonPojo) ((CrohnRadioButton) view).getPojo();
    		    	
    		    	MissedMedicationPojo pojo = (MissedMedicationPojo) CrohnContext.getPojo();
    		    	pojo.setReason(mmp.getDescription());
    		    			
    		    }
            });
	        
	        radioGroup.addView(rb);
	        if (pojo.getReason() != null && pojo.getReason().equals(record.getDescription())) {
	        	radioGroup.check(rb.getId());
	        } else {
	        }
        }
    	
    	
    }
    
}